package com.ikangtai.shecare;

import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class BBTChartActivity extends AppCompatActivity {

    private WebView webview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.bbtchart_layout);
        initView();
    }

    private final void initView(){
        webview = (WebView) findViewById(R.id.webview);

        WebSettings webSettings = webview.getSettings();
        webSettings.setJavaScriptEnabled(true);
        //webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        webview.requestFocus();
        webview.setWebViewClient(new WebViewClient());
        webview.setWebChromeClient(new WebChromeClient());

        webview.setHorizontalScrollBarEnabled(false);//水平不显示
        webview.setVerticalScrollBarEnabled(false); //垂直不显示

        //找到Html文件，也可以用网络上的文件
        webSettings.setDomStorageEnabled(true);
        webview.loadUrl("file:///android_asset/health/bbtChart.html");
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK) { //监控/拦截/屏蔽返回键
            BBTChartActivity.this.finish();
            return true;
        }

        return super.onKeyDown(keyCode, event);
    }

    //必须捕捉，否则Activity onDestroy然后onCreate，js不能有效的监听到横竖屏的变化;
    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        // 当新设置中，屏幕布局模式为横排时
        if(this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE){
            //TODO 横屏，调整webview为全屏
        }
        else if(this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT){
            //TODO 竖屏，调整webview和下半部分的比例
        }
        super.onConfigurationChanged(newConfig);
    }

    //防止内存泄露
    @Override
    protected void onDestroy(){
        webview.destroy();
        webview.removeAllViews();
        super.onDestroy();
    }
}
